package mwa.cs.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Converter {
	public List<Map<String, Object>> convertJsonToList(String json) {

		List<Map<String, Object>> voList = new ArrayList<Map<String, Object>>();
		JSONParser voParser = new JSONParser();
		Object voObj;
		try {
			voObj = voParser.parse(json);
			JSONArray voArray = (JSONArray) voObj;
			JSONObject voJObj;
			Iterator<?> voIt;
			HashMap<String, Object> voMap;
			try {
				for (int i = 0; i < voArray.size(); i++) {
					voMap = new HashMap<String, Object>();
					voJObj = new JSONObject(voArray.get(i).toString());
					voIt = voJObj.keys(); // gets all the keys
					while (voIt.hasNext()) {
						String key = (String) voIt.next(); // get key
						voMap.put(key, (String) voJObj.get(key)); // Store data to
																// HashMap
					}
					voList.add(voMap);
				}
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return voList;
	}

	@SuppressWarnings("unchecked")
	public JSONArray convertListToJsonDataTable(List<Map<String, Object>> arrayList) {

		JSONArray jsonArrayData = new JSONArray();
		JSONArray voArray = new JSONArray();
		for (int i = 0; i < arrayList.size(); i++) {
			voArray = new JSONArray();
			Iterator<String> it = arrayList.get(i).keySet().iterator();
			int j=1;
			while (it.hasNext()) {
				//String key = it.next();
				it.next();
				String value = ""+ arrayList.get(i).get(""+j);
				voArray.add((String) "" + value);
				j++;
			}
			jsonArrayData.add(voArray);
		}
		return jsonArrayData;
	}
	
	public JSONArray convertListToJsonJQGrid(List<Map<String,Object>> arrayList) {

		String json = "[";
		for (int i = 0; i < arrayList.size(); i++) {
			json += "{";
			Iterator<String> it = arrayList.get(i).keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String value = (String) arrayList.get(i).get(key);
				json += "\"" + key + "\":\"" + value + "\",";
			}
			json += "},";
		}
		json += "]";

		JSONParser parser = new JSONParser();
		Object obj;
		JSONArray array = null;
		try {
			obj = parser.parse(json);
			array = (JSONArray) obj;

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return array;
	}
	public JSONObject convertListToJson(List<Map<String,Object>> arrayList) {

		String json = "[";
		for (int i = 0; i < arrayList.size(); i++) {
			json += "{";
			Iterator<String> it = arrayList.get(i).keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String value = (String) arrayList.get(i).get(key);
				json += "\"" + key + "\":\"" + value + "\",";
			}
			json += "},";
		}
		json += "]";

		JSONParser parser = new JSONParser();
		Object obj;
		JSONObject array = null;
		try {
			obj = parser.parse(json);
			array = (JSONObject) obj;

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return array;
	}

}
